package pers.qianyu.backup.exception;

import lombok.extern.slf4j.*;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.*;
import pers.qianyu.core.http.*;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(value = BackupException.class)
    @ResponseStatus(HttpStatus.OK)
    public HttpResult backupExceptionHandler(BackupException exception) {
        log.error("NoHandlerFoundException:{}", exception.getMessage());
        return HttpResult.error(BackupLambErrorCodeEnum.BACKUP_FAILED);
    }

    @ResponseBody
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public HttpResult noHandlerFoundExceptionHandler(NoHandlerFoundException exception) {
        log.error("NoHandlerFoundException:{}", exception.getMessage());
        return HttpResult.error(BackupLambErrorCodeEnum.RESOURCES_NOT_FOUND);
    }

    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.SERVICE_UNAVAILABLE)
    public HttpResult exceptionHandler(Exception exception) {
        exception.printStackTrace();
        return HttpResult.error(BackupLambErrorCodeEnum.SYS_ERROR);
    }
}